package ClientCommands;

import java.io.IOException;


import org.json.simple.JSONObject;

import Annotations.IsClientCommand;
import CacheLayer.AbstractCache;
import CacheLayer.CacheManager;
import CacheLayer.TimeFrameCache;
import Commands.ClientCommandBase;
import Commands.ExecuteMethod;
import DomainLayer.UserEntity;
import Helpers.ApplicationLogger;
import Presenters.JSONPresenter;
import ServiceLayer.ServiceFacade;

@IsClientCommand
public class GetUserInfoById extends ClientCommandBase<JSONPresenter>{
	
	private class UserInfoJSONPresenter implements JSONPresenter	
	{
		public UserInfoJSONPresenter(UserEntity user)
		{
			if (user != null)
			{
				setFirstName(user.getUserInfo().getFirstName());
				setSecondName(user.getUserInfo().getSecondName());
				setMiddleName(user.getUserInfo().getMiddleName());
				setLogin(user.getUserInfo().getLogin());
				setGender(user.getUserInfo().getGender().toString());
				setAge(user.getUserInfo().getAge());
			}
		}

		public String getFirstName() {
			return firstName;
		}


		public String getSecondName() {
			return secondName;
		}


		public String getMiddleName() {
			return middleName;
		}


		public String getLogin() {
			return login;
		}


		public String getGender() {
			return gender;
		}


		public Integer getAge() {
			return age;
		}


		public void setFirstName(String firstName) {
			this.firstName = firstName;
		}


		public void setSecondName(String secondName) {
			this.secondName = secondName;
		}


		public void setMiddleName(String middleName) {
			this.middleName = middleName;
		}


		public void setLogin(String login) {
			this.login = login;
		}


		public void setGender(String gender) {
			this.gender = gender;
		}


		public void setAge(Integer age) {
			this.age = age;
		}

		private String firstName;
		private String secondName;
		private String middleName;
	  	private String login;
		private String gender;
		private Integer age;

		
		@Override
		public String toJSONString() {
			StringBuffer sb = new StringBuffer();                                  
			sb.append("{");                      
			sb.append("\"" + JSONObject.escape("firstName") + "\"");                 
			sb.append(":");                 
			sb.append("\"" + JSONObject.escape(getFirstName()) + "\"");                                  
			sb.append(",");                                  
			sb.append("\"" + JSONObject.escape("secondName") + "\"");
			sb.append(":");
			sb.append("\"" + JSONObject.escape(getSecondName()) + "\"");
			sb.append(",");
			sb.append("\"" + JSONObject.escape("middleName") + "\"");
			sb.append(":");
			sb.append("\"" + JSONObject.escape(getMiddleName()) + "\"");
			sb.append(",");
			sb.append("\"" + JSONObject.escape("login") + "\"");
			sb.append(":");
			sb.append("\"" + JSONObject.escape(getLogin()) + "\"");
			sb.append(",");
			sb.append("\"" + JSONObject.escape("gender") + "\"");
			sb.append(":");
			sb.append("\"" + JSONObject.escape(getGender()) + "\"");
			sb.append(",");
			sb.append("\"" + JSONObject.escape("age") + "\"");
			sb.append(":");
			sb.append("\"" + JSONObject.escape(String.valueOf(getAge())) + "\"");
			sb.append("}");                      
			return sb.toString();
		}
	}
	
	public GetUserInfoById()
	{
		setCommandName("GetUserInfoById");
		setCommandDescription("Get user info by ID");
		setExecuteMethod(ExecuteMethod.ServerOnly);
	}
	
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	String id;
	
	@Override
	protected void ExecuteInternal() throws IOException {
		JSONPresenter status = null;
		try {
			Integer cacheId = CacheManager.getInstance().CreateCache(TimeFrameCache.class, false);
			TimeFrameCache cache = (TimeFrameCache)CacheManager.getInstance().get(cacheId);
			ApplicationLogger.getInstance().LOG.debug("Manager size:" + CacheManager.getInstance().size());
			ApplicationLogger.getInstance().LOG.debug("Cache ID=" + cacheId + " SIZE: " + cache.size());
			UserEntity userEntity = (UserEntity) cache.getEntityFromCache(Integer.valueOf(getId()));
			ApplicationLogger.getInstance().LOG.debug("Look in to cache");
			if(userEntity == null){
				ApplicationLogger.getInstance().LOG.debug("Getting From DB");
				ServiceLayer.ServiceFacade sf = new ServiceFacade();
				userEntity = sf.GetUserById(Integer.valueOf(getId()));
				cache.addEntityToCache(userEntity);
				ApplicationLogger.getInstance().LOG.debug("Size of cache = " + CacheManager.getInstance().get(cacheId).size());
			}
			status = new UserInfoJSONPresenter(userEntity);
		} catch (Throwable e) {
			ApplicationLogger.getInstance().LOG.error(e.getMessage()); }
		setResult(status);
	}
	
	@Override
	public void FillFromJSON(JSONObject jObj) {
		super.FillFromJSON(jObj);
		setId((String)jObj.get("id"));
	}
	@Override
	public String toJSONString()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("{");
		sb.append("\"" + JSONObject.escape("CommandName") + "\"");                 
		sb.append(":");                 
		sb.append("\"" + JSONObject.escape(getCommandName()) + "\"");                                  
		sb.append(",");                                  
		sb.append("\"" + JSONObject.escape("CommandDescription") + "\"");                 
		sb.append(":");                 
		sb.append("\"" + getCommandDescription() + "\"");   
		sb.append(",");  
		sb.append("\"" + JSONObject.escape("ExecuteMethod") + "\"");    
		sb.append(":");
		sb.append("\"" + getExecuteMethod() + "\"");   
		sb.append(",");
		sb.append("\"" + JSONObject.escape("id") + "\"");
		sb.append(":");
		sb.append("\"" + JSONObject.escape(getId()) + "\"");
	sb.append("}");                                
		return sb.toString();
	}
}
